/**
 * Created on 26 dec. 2011 at 15:04:59 by HozakaN
 */
package fr.castorhoz.isi_connectivity_widget.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * @author HozakaN
 * 
 */
public class DBAdapter {
	private static final boolean DEBUG_APPWIDGET_LIST = true;
	private DataBaseHelper dbHelper;
	//private Context context;
	private SQLiteDatabase db;

	public DBAdapter(Context context) {
		//this.context = context;
		dbHelper = new DataBaseHelper(context);
	}

	public DBAdapter open() {
		db = dbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		db.close();
	}

	public void truncate() {
		db.execSQL("DELETE FROM " + DataBaseHelper.MOBILEDATA_STATE_TABLENAME);
		db.execSQL("DELETE FROM " + DataBaseHelper.GPS_STATE_TABLENAME);
		db.execSQL("DELETE FROM " + DataBaseHelper.WIFI_STATE_TABLENAME);
		db.execSQL("DELETE FROM " + DataBaseHelper.APPWIDGETS_WIDGETS_TABLENAME);
	}
	
	public long insertAppWidgetWidgets(String widgetName, int appWidgetId) {
		ContentValues values = new ContentValues();
		values.put(DataBaseHelper.APPWIDGETS_WIDGETS_NAME, widgetName);
		values.put(DataBaseHelper.APPWIDGET_ID, String.valueOf(appWidgetId));
		return db.insert(DataBaseHelper.APPWIDGETS_WIDGETS_TABLENAME, null,
				values);
	}

	public long insertMobileDataWidgetState(String state) {
		ContentValues values = new ContentValues();
		values.put(DataBaseHelper.MOBILEDATA_STATE_COLUMNNAME, state);
		return db.insert(DataBaseHelper.MOBILEDATA_STATE_TABLENAME, null,
				values);
	}

	public long insertGPSWidgetState(String state) {
		ContentValues values = new ContentValues();
		values.put(DataBaseHelper.GPS_STATE_COLUMNNAME, state);
		return db.insert(DataBaseHelper.GPS_STATE_TABLENAME, null, values);
	}

	public long insertWifiWidgetState(String state) {
		ContentValues values = new ContentValues();
		values.put(DataBaseHelper.WIFI_STATE_COLUMNNAME, state);
		return db.insert(DataBaseHelper.WIFI_STATE_TABLENAME, null, values);
	}

	public boolean removeWifiWidgetState(long id) {
		return db
				.delete(DataBaseHelper.WIFI_STATE_TABLENAME, "_id=?", new String[] { String.valueOf(id) }) > 0;
	}

	public boolean removeMobileDataWidgetState(long id) {
		return db.delete(DataBaseHelper.MOBILEDATA_STATE_TABLENAME,	"_id=?", new String[] { String.valueOf(id) }) > 0;
	}

	public void removeMobileDataWidgetStates() {
		db.delete(DataBaseHelper.MOBILEDATA_STATE_TABLENAME, null, null);
	}

	public void removeGPSWidgetStates() {
		db.delete(DataBaseHelper.GPS_STATE_TABLENAME, null, null);
	}

	public void removeWifiWidgetStates() {
		db.delete(DataBaseHelper.WIFI_STATE_TABLENAME, null, null);
	}
	
	public void removeAppWidgetId(int appWidgetId) {
		db.delete(DataBaseHelper.APPWIDGETS_WIDGETS_TABLENAME, DataBaseHelper.APPWIDGET_ID+" like ?", new String[] { String.valueOf(appWidgetId) });
	}

	/**
	 * Obtain MobileData state
	 * 
	 * @return
	 */
	public List<Integer> getAppWidgetIdUsingWidget(String widgetName) {
		Log.i(this.getClass().getName(), "getting appWidgetIds for widget "+widgetName);
		List<Integer> appWidgetIds = new ArrayList<Integer>();

		Log.d(this.getClass().getName(), "widgetName : "+widgetName);
		Cursor c = db.query(DataBaseHelper.APPWIDGETS_WIDGETS_TABLENAME,
				new String[] { DataBaseHelper.APPWIDGET_ID }, DataBaseHelper.APPWIDGETS_WIDGETS_NAME+" like ?",
				new String[] { widgetName }, null, null, null);
		while (c.moveToNext()) {
			appWidgetIds.add(c.getInt(c.getColumnIndexOrThrow(DataBaseHelper.APPWIDGET_ID)));
		}
		c.close();
		
		if (DEBUG_APPWIDGET_LIST) {
			StringBuilder sb = new StringBuilder();
			sb.append("contenu de la liste appWidgetIds pour le widget ").append(widgetName).append(" : ");
			for (Integer inte : appWidgetIds) {
				sb.append(inte).append(" ; ");
			}
			Log.d(this.getClass().getName(), sb.toString());
		}
		
		return appWidgetIds;
	}

	/**
	 * Obtain wifi state
	 * @return
	 */
	public Cursor getWifiWidgetState() {
		return db.query(DataBaseHelper.WIFI_STATE_TABLENAME, new String[] {
				"_id", DataBaseHelper.WIFI_STATE_COLUMNNAME }, null, null,
				null, null, null);
	}

	/**
	 * Obtain MobileData state
	 * 
	 * @return
	 */
	public Cursor getMobileDataWidgetState() {
		return db.query(DataBaseHelper.MOBILEDATA_STATE_TABLENAME,
				new String[] { "_id",
						DataBaseHelper.MOBILEDATA_STATE_COLUMNNAME }, null,
				null, null, null, null);
	}

	/**
	 * Obtain GPS state
	 * 
	 * @return
	 */
	public Cursor getGPSWidgetState() {
		return db.query(DataBaseHelper.GPS_STATE_TABLENAME, new String[] {
				"_id", DataBaseHelper.GPS_STATE_COLUMNNAME }, null, null, null,
				null, null);
	}

}
